package remoting

import (
	"bbq/common/protocol/remoteCommand"
	"time"
)

type ResponseFuture struct {
	Opaque int64

	CmdChannel chan *remoteCommand.RemoteCommand
}

func (rf *ResponseFuture) WaitResponse(timeout time.Duration) *remoteCommand.RemoteCommand {
	select {
	case cmd := <-rf.CmdChannel:
		return cmd
	case <-time.After(timeout):
		return nil
	}
}

func (rf *ResponseFuture) PutResponse(cmd *remoteCommand.RemoteCommand) {
	rf.CmdChannel <- cmd
	close(rf.CmdChannel)
}

func NewResponseFuture(opaque int64) *ResponseFuture {
	return &ResponseFuture{
		Opaque:     opaque,
		CmdChannel: make(chan *remoteCommand.RemoteCommand, 1),
	}
}
